package com.ruoyi.reservationserver.controller.admin;


import com.ruoyi.reservationserver.config.JwtConfig;
import com.ruoyi.reservationserver.pojo.LoginPojo;
import com.ruoyi.reservationserver.service.AdminService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("admin")
public class LoginController {
    @Resource
    private JwtConfig jwtConfig;

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    //    public Object login(String account, String password, String role) {
    //@RequestBody 表示接收是的Json格式的参数
    public Object login(@RequestBody LoginPojo loginPojo) {
        System.out.println("login:" + loginPojo);
        Object bean = null;

        bean = adminService.login(loginPojo.getAccount(), loginPojo.getPassword(),loginPojo.getRole());

        if (bean == null) {
            return Result.error("登录失败");
        }else {
            String token = jwtConfig.createToken(loginPojo.getAccount());
            return Result.success(token);

        }
    }
}
